<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-row type="flex" justify="center" align="middle">
      <el-col :span="10">
        <el-form ref="form" :model="passwordData" :rules="rules" label-width="100px" size="large">
          <el-form-item label="用户名">
            <el-input v-model="passwordData.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="旧密码" prop="oldPwd">
            <el-input type="password"
                      ref="oldPwd"
                      placeholder="请输入旧密码"
                      :type="passwordType"
                      v-model="passwordData.oldPwd"
            />
          </el-form-item>

          <el-form-item label="新密码" prop="newPwd">
            <el-input type="password"
                      ref="password"
                      placeholder="请输入新密码"
                      :type="passwordType"
                      v-model="passwordData.newPwd"
            />
          </el-form-item>

          <el-form-item prop="rePwd">
            <el-input type="password"
                      ref="password"
                      placeholder="请再次输入新密码"
                      :type="passwordType"
                      v-model="passwordData.rePwd"
            />
          </el-form-item>
          <el-form-item>
            <el-button v-if="passwordType" type="primary" @click="showPwd">显示密码</el-button>
            <el-button v-else type="info" @click="showPwd">隐藏密码</el-button>
            <el-button type="success" @click="resetPassword">重置密码</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
import api from '@/api/user/user'

export default {

  data() {
    return {
      passwordData: {
        username: this.$store.getters.name,
        oldPwd: '',
        newPwd: '',
        rePwd: ''
      },

      rules: {
        oldPwd: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        rePwd: [
          { validator: this.checkRePwd, trigger: 'blur' }
        ]
      },

      passwordType: 'password'

    }
  },
  methods: {
    // 显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 校验重新输入的密码
    checkRePwd(rule, value, callback) {
      if (value == null || value === '') {
        callback(new Error('再次输入的密码不能为空'))
      } else if (value !== this.passwordData.newPwd) {
        callback(new Error('请确保两次输入的密码一样'))
      } else {
        callback()
      }
    },

    // 重置密码
    resetPassword() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 校验通过
          api.updatePwd(this.passwordData).then(async response => {
            // 跳转到登录页面
            await this.$store.dispatch('user/logout')
            this.$router.push(`/login?redirect=${this.$route.fullPath}`)
            //提示信息
            this.$message({
              type: 'success',
              message: '密码重置成功，请重新登录!'
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray: #889aa4;
.page-container {
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  margin: 50px;
  //height: 500px;

}
</style>
